import { Toast } from 'antd-mobile';
import pathToRegexp from 'path-to-regexp';
import { routerRedux } from 'dva/router';
import { Route } from 'dva/router';
import {
    distributionIndex,
    incomeDetail,
    myTeamDetail,
    joinStore,
    editStore,
    putForward,
    putForwardCash,
    myStoreDetail,
    myStoreOrderDetail,
    reportDetail,
    distributeSearch,
    statistics,
    meberDetail,
    delGoods
} from "./api";
import {parse} from "query-string";
import {getYE} from "../my/api";




export default {
    namespace: 'distribution',
    state: {
        pageNo:1,
        distributorProductVoList:[],
        carouselList:[],
        goodsType:[],
        incomeDetailList:[],
        distributeProductlist:[],
        myStoreDetailList:[],
        myStoreOrderList:[],
        statisticsList:[],
        myTeamList:[],
        sortField:"",
        sortOrder:"",
        keyword:"",
        orderState:"1002",
    },
    reducers: {
        save(state, {data}) {
            return {...state, ...data};
        },
        clearState() {
            return {
                distributorProductVoList:[],
                distributeProductlist:[],
                myStoreDetailList:[],
                carouselList:[],
                statisticsList:[],
                myStoreOrderList:[],
                goodsType:[],
                incomeDetailList:[],
                myTeamList:[],
            };
        }

    },
    effects: {
        // 分销首页
        *distributionIndexEFF({ obj }, { put, call, select }) {
            const distribution = yield select((state)=>state.distribution);
            const result = yield call(distributionIndex, { ...obj });
            if (result.code == 200) {
                if(result.ret==""){yield put({ type: 'save', data: { carouselList:[],goodsType:[]} })}else{
                   yield put({ type: 'save', data: {carouselList:result.ret.carouselList,goodsType:result.ret.goodsType} });
                }
            }
            return result
        },
        //分销搜索
        *distributeSearchEFF({ obj }, { put, call, select }) {
            const distribution = yield select((state)=>state.distribution);
            const result = yield call(distributeSearch, { keyword:distribution.keyword,sortField:distribution.sortField,sortOrder:distribution.sortOrder, pageNo:distribution.pageNo, ...obj });
            if (result.code == 200) {
                yield put({ type: 'save', data: result.ret  });
                if(result.ret==""){yield put({ type: 'save', data: { distributorProductVoList: []} })}else{
                    if(result.ret.pageNo <= 1) yield put({ type: 'save', data: {...result.ret, distributorProductVoList: result.ret.distributorProductVoList} });

                    else yield put({ type: 'save', data: { ...result.ret,distributorProductVoList: [...distribution.distributorProductVoList, ...result.ret.distributorProductVoList]} });


                }
            }
            return result
        },
        //分销订单
        *myStoreOrderDetailEFF({ obj }, { put, call, select }) {
            const distribution = yield select((state)=>state.distribution);
            const result = yield call(myStoreOrderDetail, { orderState:distribution.orderState, pageNo:distribution.pageNo, ...obj });
            if (result.code == 200) {
                yield put({ type: 'save', data: result.ret  });
                if(result.ret==""){yield put({ type: 'save', data: { myStoreOrderList: []} })}else{
                    if(result.ret.pageNo <= 1) yield put({ type: 'save', data: {...result.ret, myStoreOrderList: result.ret.list} });

                    else yield put({ type: 'save', data: { ...result.ret,myStoreOrderList: [...distribution.myStoreOrderList, ...result.ret.list]} });


                }
            }
            return result
        },
        // 收入详情
        *incomeDetailEFF({ obj }, { put, call, select }) {
            const distribution = yield select((state)=>state.distribution);
            const result = yield call(incomeDetail, { incomeAndExpenditureType:distribution.incomeAndExpenditureType, pageNo:distribution.pageNo, ...obj });
            if (result.code == 200) {
                yield put({ type: 'save', data: result.ret  });
                console.log(result)
                if(result.ret==""){yield put({ type: 'save', data: { incomeDetailList: []} })}else{
                    if(result.ret.pageNo <= 1) yield put({ type: 'save', data: {...result.ret, incomeDetailList: result.ret.list} });

                    else yield put({ type: 'save', data: { ...result.ret,incomeDetailList: [...distribution.incomeDetailList, ...result.ret.list]} });

                }
            }
            return result
        },
        // 我的团队
        *myTeamDetailEFF({ obj }, { put, call, select }) {
            const distribution = yield select((state)=>state.distribution);
            const result = yield call(myTeamDetail, {pageNo:distribution.pageNo, ...obj });
            if (result.code == 200) {
                yield put({ type: 'save', data: result.ret  });
                if(result.ret==""){yield put({ type: 'save', data: { myTeamList: []} })}else{
                    if(result.ret.pageNo <= 1) yield put({ type: 'save', data: {...result.ret, myTeamList: result.ret.list} });

                    else yield put({ type: 'save', data: { ...result.ret,myTeamList: [...distribution.myTeamList, ...result.ret.list]} });

                }
            }
            return result
        },

        //我的小店详情页面
        *myStoreDetailEFF({ obj }, { put, call, select }) {
            const distribution = yield select((state)=>state.distribution);
            const result = yield call(myStoreDetail, { sortField:distribution.sortField,sortOrder:distribution.sortOrder, pageNo:distribution.pageNo, ...obj });
            if (result.code == 200) {
                yield put({ type: 'save', data: result.ret  });
                if(result.ret==""){yield put({ type: 'save', data: { myStoreDetailList: []} })}else{
                    if(result.ret.myStorelist.pageNo <= 1) yield put({ type: 'save', data: {...result.ret, myStoreDetailList: result.ret.myStorelist.list} });

                    else yield put({ type: 'save', data: { ...result.ret,myStoreDetailList: [...distribution.myStoreDetailList, ...result.ret.myStorelist.list]} });


                }
            }
            return result
        },
        *myDetailEFF({ }, { put, call }) {
            console.warn("进入 myDetailEFF")
            const result = yield call(meberDetail);
            console.log(result)
            if (result.code == 200) {
                yield put({ type: 'save', data: {dataList:result.ret} });
                Toast.hide();
            }
        },
        *reportDetailEFF({ }, { put, call }) {
            console.warn("进入 ")
            const result = yield call(reportDetail);
            console.log(result)
            if (result.code == 200) {
                yield put({ type: 'save', data: {reportDetailList:result.ret} });
                Toast.hide();
            }
        },
        *statisticsEFF({ obj}, { put, call }) {
            console.warn("进入 ")
            const result = yield call(statistics,obj);
            console.log(result)
            if (result.code == 200) {
                yield put({ type: 'save', data: {statisticsList:result.ret} });
                Toast.hide();
            }
        },
        //可提现金额
        *putForwardEFF({ }, { put, call, select }) {
            const result = yield call(putForward);
            console.log(result);
            if (result.code == 200) {
                yield put({ type: 'save', data: result.ret  });
            }
        },
        // 加入小店
        *joinStoreEFF({obj}, {put, call}) {
            // Toast.loading('加载中...', 3);
            const result = yield call(joinStore,obj);
            return result;
        },
        *delGoodsEFF({obj}, {put, call}) {
            // Toast.loading('加载中...', 3);
            const result = yield call(delGoods,obj);
            return result;
        },
        //提现
        *putForwardCashEFF({obj}, {put, call}) {
            // Toast.loading('加载中...', 3);
            const result = yield call(putForwardCash,obj);
            return result;
        },
        // 修改小店信息
        *editStoreEFF({obj}, {put, call}) {
            // Toast.loading('加载中...', 3);
            const result = yield call(editStore,obj);
            return result;
        },
        subscriptions: {
            setup({ dispatch, history }) {
                return history.listen(({ pathname, query }) => {
                    if(pathname.includes("/distribution")||pathname.includes("/distributionSearch")||pathname.includes("/myDistribution")||pathname.includes("/myDistributionOrder")){
                        dispatch({type:"clearState"})
                    }
                    if(pathname.includes("/putForward")){
                        dispatch({type:"putForwardEFF"})
                    }
                    if(pathname.includes("/reportDetail")){
                        dispatch({type:"reportDetailEFF"})
                    }
                });
            },
        }
    },

}